package net.wanho.ums.controller;

import cn.hutool.json.JSONUtil;
import net.wanho.ums.entity.User;
import net.wanho.ums.service.UserService;
import net.wanho.ums.service.impl.UserServiceImpl;
import net.wanho.ums.vo.AjaxResult;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Author：汤小洋
 * Date：2023-09-15 10:50
 * Description：<描述>
 */
@WebServlet("/register")
public class UserController extends HttpServlet {

    private UserService userService = new UserServiceImpl();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 1.接收参数
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        int age = Integer.parseInt(req.getParameter("age"));
        // 2.封装对象
        User user = new User(null, username, password, age);
        // 3.调用业务方法
        userService.register(user);

        resp.setContentType("application/json;charset=utf8");

        AjaxResult result = new AjaxResult();
        result.setStatus(200);
        result.setMsg("注册成功");

        resp.getWriter().write(JSONUtil.toJsonStr(result));
    }
}
